<?php

include DIRETORIO."app/Models/BacklogDAO.php";

class C_Backlog{

	protected static $lk = NULL;
		
	public static function init(){
		global $db_Mysql;

		self::$lk = $db_Mysql;
	}

	public static function getDB(){
		if(self::$lk == NUll)
			self::init();

		return self::$lk;
	}

	public static function getBacklog(){
		$return = null;

		$objBacklogDAO	= new BacklogDAO(self::getDB());
		$objBacklog		= $objBacklogDAO->getBacklog(0, $_SESSION["PROJECT"]->id);		
		
		if(count($objBacklog) > 0){
			$html = '<ul>';

			foreach($objBacklog as $row){

				$description = $row->description;

				if(strlen($description) > 100){
					$description = substr($description, 0, 100)." ...";
				}

				if($_SESSION["PROJECT"]->status < 3 && $_SESSION["USER_PERMISSION"] == 1){
					$buttons = '<a onclick="deleteBacklog('.$row->id.')" href="javascript:void(0);" class="icon delete"><span>delete</span></a>';
					$buttons .= '<a onclick="editBacklog('.$row->id.')" href="javascript:void(0);" class="icon edit"><span>edit</span></a>';
				}else{
					$buttons = '<a onclick="viewBacklog('.$row->id.')" href="javascript:void(0);" class="icon view"><span>view</span></a>';
				}

				$nameUserCreator = strstr($row->nameUserCreator, ' ', true);
				
				$html .= '	<li>
								<div class="task" id="backlog_'.$row->id.'">
									<span class="title_bar">
										<span class="count_tasks">Tarefas: '.$row->tasks.'/'.$row->tasksFinish.'</span>
										<span class="status '.$row->priorityClass.'" title="'.$row->priority.'"></span>
										<span class="user">'.$nameUserCreator.'</span>
									</span>
									<span class="title">'.$row->name.'.<button class="expand">+</button></span>
									<div class="details"><span>'.$description.'</span></div>
									<div class="buttons">										
										'.$buttons.'
									</div>
								</div>
							</li>';
			}

			$html .= '</ul>';
			$return[] = true;
		}else{
			$html = "<h2>Nenhum registro encontrado!</h2>";
			$return[] = false;
		}
	
		$return[] = $html;	
		return json_encode($return);
	}

	public static function newBacklog(){
		$return = null;

		$html = file_get_contents(TEMPLATES."backlog.html");
		
		$html = str_replace("[TITLE]","Novo Item de Backlog",$html);
		$html = str_replace("[ID_BACKLOGITEM]",0,$html);
		$html = str_replace("[NAME]","",$html);
		$html = str_replace("[PRIORITY]","",$html);
		$html = str_replace("[PRIORITY_CLASS]","",$html);
		$html = str_replace("[DESCRIPTION]","",$html);

		if($html <> ""){
			$return = array(true,$html);
		}else{
			$return = array(false,"Não foi possível carregar o novo Item de Backlog!");
		}

		return json_encode($return);
	}

	public static function editBacklog($post){
		$return = null;
		
		$objBacklogDAO	= new BacklogDAO(self::getDB());
		$objBacklog		= $objBacklogDAO->getBacklog((int)$post[id]);

		$html = file_get_contents(TEMPLATES."backlog.html");
		$html = str_replace("[TITLE]","Editar Item de Backlog",$html);
		$html = str_replace("[ID_BACKLOGITEM]",$objBacklog[0]->id,$html);
		$html = str_replace("[NAME]",$objBacklog[0]->name,$html);
		$html = str_replace("[PRIORITY]",$objBacklog[0]->priority,$html);
		$html = str_replace("[PRIORITY_CLASS]",$objBacklog[0]->priorityClass,$html);
		$html = str_replace("[DESCRIPTION]",$objBacklog[0]->description,$html);

		if($html <> ""){
			$return = array(true,$html);
		}else{
			$return = array(false,"Não foi possível editar o Item de Backlog!");
		}

		return json_encode($return);
	}

	public static function viewBacklog($post){
		$return = null;
		
		$objBacklogDAO	= new BacklogDAO(self::getDB());
		$objBacklog		= $objBacklogDAO->getBacklog((int)$post[id]);

		$html = file_get_contents(TEMPLATES."viewBacklog.html");
		$html = str_replace("[TITLE]","Visualização do Item de Backlog",$html);
		$html = str_replace("[NAME]",$objBacklog[0]->name,$html);
		$html = str_replace("[PRIORITY]",$objBacklog[0]->priority,$html);
		$html = str_replace("[PRIORITY_CLASS]",$objBacklog[0]->priorityClass,$html);
		$html = str_replace("[DESCRIPTION]",$objBacklog[0]->description,$html);

		if($html <> ""){
			$return = array(true,$html);
		}else{
			$return = array(false,"Não foi possível visualizar o Item de Backlog!");
		}

		return json_encode($return);
	}

	public static function saveBacklog($post){
		$return = null;
		$id		= 0;
		
		if(count($post) > 0){
			$objBacklogDAO	= new BacklogDAO(self::getDB());		
			$objBacklog		= new Backlog();

			foreach($post as $key => $value){
				$objBacklog->$key = $value;
			}

			$objBacklog->idUserCreator	= $_SESSION["USER"]->id;
			$objBacklog->idProject		= $_SESSION["PROJECT"]->id;
			$objBacklog->status			= 1; //Ativa
			
			if($objBacklog->id > 0){		
				$objBacklogDAO->save($objBacklog);
				$id = $objBacklog->id;
			}else{
				$id = $objBacklogDAO->save($objBacklog);
			}
		}
		

		if($id > 0){
			$return =  array(true,"Item \"<i>". $objBacklog->name ."</i>\" salvo com sucesso");
		}else{
			$return =  array(false,"Não foi possível salvar o Item de Backlog!");
		}


		return json_encode($return);
	}

	public static function getTaskBacklog(){
		$return = array(false,"Não foi possível atualizar os Itens de Backlog!");

		$objBacklogDAO	= new BacklogDAO(self::getDB());
		$result = $objBacklogDAO->getTaskBacklog($_SESSION["PROJECT"]->id);

		if(count($result) > 0){
			$return = array(true,$result);
		}

		return json_encode($return);
	}

	public static function deleteBacklog($post){	
		$objBacklogDAO	= new BacklogDAO(self::getDB());
		$idBacklog		= (int)$post['id'];
		$result			= 0;

		if($idBacklog > 0)
			$result = $objBacklogDAO->deleteBacklog($idBacklog);

		if($result > 0)
			$return = array(true,"Item de Backlog excluído com sucesso!");
		else if($result == -1)
			$return = array(false,"Erro, existem tarefas vinculadas a este Item de Backlog!");
		else
			$return = array(false,"Erro, não foi possível excluir o Item de Backlog!");

		return json_encode($return);
	}
}
?>